/* REVISION: 9.1       LAST MODIFIED: 10/06/03   BY: *CLI* cli */
/* Revision: 9.1       LAST MODIFIED: 10/28/03   BY: *clicli* cli   SPEC: HK-S-014*/
/* REVISION: 9.1       LAST MODIFIED: 11/12/03   BY: *cli-cli* Joy Huang */
/* REVISION: 9.1       LAST MODIFIED: 02/19/04   BY: *02/19/04* Joy Huang */

/*V8:ConvertMode=Maintenance                                              */


{mfdtitle.i "b+ "}
/* {mfdeclre.i} */

define new shared variable nbr like rqm_mstr.rqm_nbr.
define new shared variable nbr1 like rqm_mstr.rqm_nbr.
define new shared variable vend like rqm_vend.
define new shared variable vend1 like rqm_vend.
define new shared variable site like rqm_site.
define new shared variable release_all like mfc_logical.
define new shared variable grs_req_nbr     like req_nbr no-undo.
define new shared variable grs_approval_cntr as integer no-undo.
define variable l_req_nbr       like rqm_mstr.rqm_nbr no-undo.
define variable grs_return_code as   integer          no-undo.
define variable remarks_text    like rqm_rmks         no-undo.
define variable new_req as logical no-undo.
define variable line as integer no-undo.
define variable email_sent_to as character no-undo.
define new shared variable userid_modifying as character no-undo.
define variable ACTION_DELETE_REQ      as character no-undo initial '9'.
/*clicli*/ define variable nrm-seq-num like so_nbr init "".
/*clicli*/ define variable nrmnbr     like nr_seqid. 

/*cli-cli*/ define variable hk_curr like rqm_curr no-undo.

define new shared temp-table tt-rqm-mstr no-undo
    field tt-site   like rqm_mstr.rqm_site
    field tt-vend   like rqm_mstr.rqm_vend
    field tt-nbr    like rqm_mstr.rqm_nbr
    field tt-prnbr  like rqm_mstr.rqm_nbr
    FIELD tt-buyer LIKE rqm_mstr.rqm_buyer
    FIELD tt-open  LIKE rqm_mstr.rqm_open
    FIELD tt-rtdto-purch LIKE rqm_mstr.rqm_rtdto_purch
    field tt-prline   like rqd_det.rqd_line
    field tt-part   like rqd_det.rqd_part
    field tt-rel-date like rqd_rel_date
    field tt-due-date like rqd_due_date
    field tt-qty-ord like rqd_req_qty
    field tt-error as logical init Yes
/*cli-cli*/ field tt-need-date like rqd_need_date
/*cli-cli*/ field tt-curr like rqm_mstr.rqm_curr
/*cli-cli*/ field tt-recid as recid
    index vend is primary
       tt-site
       tt-vend
       tt-nbr
    index ttnbr
      tt-site 
      tt-nbr.

define buffer b-rqm-mstr for tt-rqm-mstr.
/*cli-cli*/ define variable prdiv_hk_pct like prdiv_pct.
/*cli-cli*/ define new shared variable cli_rqm_nbr  like rqm_mstr.rqm_nbr.

/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
            
 RECT-FRAME       AT ROW 1.4 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1   COLUMN 3 NO-LABEL
 SKIP(.1)  /*GUI*/
            nbr             colon 20
            nbr1            colon 55 label {t001.i}
/*cli-cli   vend            colon 20
            vend1           colon 55 label {t001.i}*/
            site            colon 20
          SKIP(.4)  /*GUI*/
with frame a width 80 attr-space side-labels NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/


/*GUI preprocessor Frame A undefine */
 &UNDEFINE PP_FRAME_NAME

/* SET EXTERNAL LABELS */
 setFrameLabels(frame a:handle).

      view frame a.
      mainloop:
      repeat with frame a:
/*GUI*/ if global-beam-me-up then undo, leave.
        set   nbr
              nbr1
/*cli-cli     vend
              vend1*/
              site.
        
           find si_mstr where input site = si_site no-lock no-error.
           if not available si_mstr then do: 
              {pxmsg.i &MSGNUM=708 &ERRORLEVEL=3}
              next-prompt site with frame a.
              undo mainloop, retry.
           end. 
       
          remarks_text = getTermLabel("MRP_PLANNED_ORDER",23).

          find first gl_ctrl no-lock no-error.
          for each tt-rqm-mstr:
              delete tt-rqm-mstr.
          end.  
          
/*cli-cli delete**************************************************************************          
                                                               
          FOR EACH rqd_det WHERE (rqd_nbr >= nbr and rqd_nbr <= nbr1) or                                  
                                 (rqd_vend>= vend and rqd_vend <= vend1) and                              
                                  rqd_site = site and                                                     
                                  rqd_aprv_stat = "2" and                                                
                                  rqd_status = "" and                                                     
                                  rqd__chr01 = "" no-lock:             
                                   
             FIND FIRST prdiv_mstr WHERE prdiv_site = rqd_site AND prdiv_part = rqd_part NO-LOCK NO-ERROR.
             IF AVAILABLE prdiv_mstr THEN DO:                     
             
                if prdiv_ord_min > (rqd_req_qty * prdiv_pct / 100) or 
                   prdiv_ord_min2 > (rqd_req_qty * prdiv_pct2 / 100) or
                   prdiv_ord_min3 > (rqd_req_qty * prdiv_pct3 / 100) or
                   prdiv_ord_min4 > (rqd_req_qty * prdiv_pct4 / 100) or
                   prdiv_ord_min5 > (rqd_req_qty * prdiv_pct5 / 100) then next. 
                                                                                                 
                IF prdiv_vend <> "" THEN DO:      
                                                                           
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend       
                          tt-prnbr = rqd_nbr
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site                                                              
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct) / 100.                                   
                END.                                                                                      
                IF prdiv_vend2 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend2    
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site                                                              
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct2) / 100.                                  
                END.                                                                                      
                IF prdiv_vend3 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend3     
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site                                                           
                          tt-rel-date = rqd_rel_date                                                   
                          tt-due-date = rqd_due_date                                                   
                          tt-qty-ord = (rqd_req_qty * prdiv_pct3) / 100.                               
                END.                                                                                      
                IF prdiv_vend4 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend4 
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site                                                              
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct4) / 100.                                  
                END.                                                                                      
                IF prdiv_vend5 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend5                  
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site                                                              
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct5) / 100.                                  
                END.                                                                                      
             END. /* IF AVAILABLE prdiv_mstr */

*cli-cli end**************************************************************************/
                               
/*cli-cli begin**************************************************************************/

          FOR EACH rqm_mstr WHERE  (rqm_nbr >= nbr and rqm_nbr <= nbr1),
              EACH rqd_det WHERE rqd_nbr = rqm_nbr and                                  
                                  rqd_vend = "" and                              
                                  rqd_site = site and                                                     
                                  rqd_aprv_stat = "2" and                                                
                                  rqd_status = "" and                                                     
                                  rqd__chr01 = "" no-lock:             
                                   
             FIND FIRST prdiv_mstr WHERE prdiv_site = rqd_site AND prdiv_part = rqd_part NO-LOCK NO-ERROR.
             IF AVAILABLE prdiv_mstr THEN DO:                     
             
                if prdiv_ord_min > rqd_req_qty then do:
                   assign prdiv_hk_pct = max(prdiv_pct,max(prdiv_pct2,max(prdiv_pct3,max(prdiv_pct4,prdiv_pct5)))).                   
                   
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part       
                          tt-prnbr = rqd_nbr
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site
                          tt-buyer = rqm_buyer
                           tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = rqd_req_qty
/*cli-cli*/               tt-need-date = rqd_need_date.
                     if prdiv_hk_pct = prdiv_pct  then tt-vend = prdiv_vend.
                     if prdiv_hk_pct = prdiv_pct2 then tt-vend = prdiv_vend2.
                     if prdiv_hk_pct = prdiv_pct3 then tt-vend = prdiv_vend3.
                     if prdiv_hk_pct = prdiv_pct4 then tt-vend = prdiv_vend4.
                     if prdiv_hk_pct = prdiv_pct5 then tt-vend = prdiv_vend5.      

/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.
 
/******************************02/19/04 end*********************************************/

              end.                                                                                                                                        
                else do:               


                IF prdiv_vend <> "" THEN DO:      
                                                                           
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend       
                          tt-prnbr = rqd_nbr
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site 
                          tt-buyer = rqm_buyer
                          tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct) / 100
/*cli-cli*/               tt-need-date = rqd_need_date.

/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.

 
/******************************02/19/04 end*********************************************/


                END.                                                                                      
                IF prdiv_vend2 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend2    
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site
                          tt-buyer = rqm_buyer
                          tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct2) / 100
/*cli-cli*/               tt-need-date = rqd_need_date.

/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.

 /******************************02/19/04 end*********************************************/


                END.                                                                                      
                IF prdiv_vend3 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend3     
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site
                          tt-buyer = rqm_buyer
                          tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                   
                          tt-due-date = rqd_due_date                                                   
                          tt-qty-ord = (rqd_req_qty * prdiv_pct3) / 100
/*cli-cli*/               tt-need-date = rqd_need_date.

/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.
/******************************02/19/04 end*********************************************/


                END.                                                                                      
                IF prdiv_vend4 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend4 
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site
                          tt-buyer = rqm_buyer
                          tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct4) / 100
/*cli-cli*/               tt-need-date = rqd_need_date.


/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.
/******************************02/19/04 end*********************************************/


                END.                                                                                      
                IF prdiv_vend5 <> "" THEN DO:                                                             
                   CREATE tt-rqm-mstr.                                                                    
                   ASSIGN tt-part = rqd_part                                                              
                          tt-vend = prdiv_vend5                  
                          tt-prnbr = rqd_nbr                                                    
                          tt-prline = rqd_line                                                    
                          tt-site = rqd_site
                          tt-buyer = rqm_buyer
                          tt-open = rqm_open
                          tt-rtdto-purch = rqm_rtdto_purch
                          tt-rel-date = rqd_rel_date                                                      
                          tt-due-date = rqd_due_date                                                      
                          tt-qty-ord = (rqd_req_qty * prdiv_pct5) / 100
/*cli-cli*/               tt-need-date = rqd_need_date.

/******************************02/19/04 add*********************************************/
/*mage*/             tt-recid = ?.
                     find vd_mstr no-lock where vd_addr = tt-vend no-error.
                         if avail vd_mstr then tt-curr = vd_curr.


                      hk_curr = "".
                      for each pc_mstr no-lock 
                          where pc_list = tt-vend and pc_part = tt-part
                            and pc_user1 = tt-site and (pc_start < rqd_need_date 
                            and (pc_expire > rqd_need_date or pc_expire = ?))
                              and pc_amt_type = "L" break by pc_start desc:
 
                           if pc_curr = tt-curr then do:
                              if pc_um = rqd_um then do:
                                 assign tt-recid = recid(pc_mstr).
                                 hk_curr = "".
                                 leave.
                              end.
                              else do:
                              assign tt-recid = recid(pc_mstr).
                              hk_curr = "".
                              end.
                              end.
                              ELSE IF pc_um = rqd_um THEN DO:
                                assign tt-recid = recid(pc_mstr).
                                        hk_curr = pc_curr.
                              END.
/*mage*/		   if last-of(pc_start) then do:
                                          if tt-recid = ? then do:
										    assign tt-recid = recid(pc_mstr).
                                           hk_curr = pc_curr. 
										   end.
                                          leave.
                       end.
/*mage*/                      end. 
                       if hk_curr <> "" then tt-curr = hk_curr.
/******************************02/19/04 end*********************************************/



                END.
               end. /*else do:*/                                                                                       
             END. /* IF AVAILABLE prdiv_mstr */
             
/*cli-cli end**************************************************************************/
                                       
                                                                             
/*GUI*/      if global-beam-me-up then undo, leave.                                                   
          END. /* for each rqd_det */                                                                     

                                                                                                          
          for each tt-rqm-mstr where tt-nbr = ""                                                          
              use-index ttnbr                                                                             
              exclusive-lock break by tt-vend by tt-curr:
                                                                                                          
              for first b-rqm-mstr where b-rqm-mstr.tt-site = tt-rqm-mstr.tt-site AND                     
                                         b-rqm-mstr.tt-vend = tt-rqm-mstr.tt-vend and
/*cli-cli*/                              b-rqm-mstr.tt-curr = tt-rqm-mstr.tt-curr and
                                         b-rqm-mstr.tt-nbr  <> ""                                         
                                         use-index vend no-lock:                                          
              end. /* FOR FIRST b-rqm-mstr */                                                             
              if not available b-rqm-mstr then do: 
                                                                     
/*clicli**  Begin ***** Generate a new Global Requistion Number from NRM *****/ 
                 find first nrs_mstr where nrs_site = input site and 
                      nrs_prnbr <> "" no-lock no-error.
                 if available nrs_mstr then do:  
                    assign nrmnbr = nrs_prnbr
                           nrm-seq-num = "".  
                    if nrm-seq-num = "" then do:
                       {gprunp.i "xxnrm" "p" "nr_dispense"
                        "(input nrmnbr,
                          input TODAY,
                          output nrm-seq-num)"}
                    end. /* IF nrm-seq-num = " " */
                    if nrm-seq-num = "" then do:
                       {gprunmo.i                                                                               
                        &program="mrprapa1.p"                                                                
                        &module="GRS"                                                                        
                        &param="""(output grs_return_code,                                                   
                                   output grs_req_nbr)"""}                                                   
                       if grs_return_code = 0 then                                                             
                          assign                                                                               
                          tt-rqm-mstr.tt-nbr = grs_req_nbr.                                                 
                    end.
                    else 
                       assign tt-rqm-mstr.tt-nbr = nrm-seq-num.
                 end.
                 else do:
                       {gprunmo.i                                                                               
                        &program="mrprapa1.p"                                                                
                        &module="GRS"                                                                        
                        &param="""(output grs_return_code,                                                   
                                   output grs_req_nbr)"""}                                                   
                       if grs_return_code = 0 then                                                             
                          assign                                                                               
                          tt-rqm-mstr.tt-nbr = grs_req_nbr.                                                 
                 end.               
/*clicli** Ending *** Generate a new Global Requistion Number from NRM ****/

/*clicli*
*                       {gprunmo.i                                                                               
*                        &program="mrprapa1.p"                                                                
*                        &module="GRS"                                                                        
*                        &param="""(output grs_return_code,                                                   
*                                   output grs_req_nbr)"""}                                                   
*                       if grs_return_code = 0 then                                                             
*                          assign                                                                               
*                          tt-rqm-mstr.tt-nbr = grs_req_nbr.                                                 
**clicli*/   
              end. /* IF NOT AVAILABLE b-rqm-mstr */                                                    
              else do:                                                                                    
                  l_req_nbr = b-rqm-mstr.tt-nbr.                                                          
                  find first b-rqm-mstr                                                                   
                     where b-rqm-mstr.tt-vend = tt-rqm-mstr.tt-vend                                       
                     and   b-rqm-mstr.tt-nbr  = ""                                                        
                  use-index vend                                                                          
                  exclusive-lock no-error.                                                                
                  if available b-rqm-mstr                                                                 
                  then                                                                                    
                     assign                                                                               
                        b-rqm-mstr.tt-nbr = l_req_nbr.                                                    
              end. /* ELSE DO */                                                                          
          end. /* FOR EACH tt-rqm-mstr */                                                                 

          for each tt-rqm-mstr                                                                              
              where tt-rqm-mstr.tt-vend <> ""                                                              
              use-index ttnbr:                                                                                                            
                                                                                                           
              grs_req_nbr = tt-rqm-mstr.tt-nbr.                                                            
/*cli-cli*/   cli_rqm_nbr = tt-rqm-mstr.tt-prnbr.                                                                                             
              
	      for first rqm_mstr                                                                           
                 where rqm_nbr  = tt-rqm-mstr.tt-nbr and                                                   
                       rqm_vend = tt-rqm-mstr.tt-vend no-lock:                                                
              end. /* FOR FIRST rqm_mstr */                                                                
              if not available rqm_mstr then                                                               
                 grs_approval_cntr = 0.                                                                    
              /* ADD +1 TO THE COUNTER */                                                                  
              /* OF RECORDS SELECTED   */                                                                                              
              grs_approval_cntr = grs_approval_cntr + 1.                                                   
              {gprunmo.i                                                                                   
               &program="xxprsplit2.p"                                                                     
               &module="GRS"                                                                               
               &param="""(input grs_approval_cntr,                                                         
                          input grs_req_nbr,                                                               
                          input tt-rqm-mstr.tt-part,                                                       
                          input tt-rqm-mstr.tt-site,
                          INPUT tt-rqm-mstr.tt-buyer,
                           INPUT tt-rqm-mstr.tt-open,
                           INPUT tt-rqm-mstr.tt-rtdto-purch,
                           input tt-rqm-mstr.tt-vend,                                                       
                          input tt-rqm-mstr.tt-qty-ord,                                                    
                          input tt-rqm-mstr.tt-rel-date,                                                   
                          input tt-rqm-mstr.tt-due-date,                                                   
/*cli-cli*/               input tt-rqm-mstr.tt-curr,
                          input remarks_text,
/*cli-cli*/               input tt-rqm-mstr.tt-recid,
                          output grs_return_code)"""}                                                      
              if grs_return_code <> 0 then do:                                                             
                    undo, leave.                                                                           
              end. /* if grs_return_code <> 0 */ 
              else do:
                    assign tt-rqm-mstr.tt-error = No.
              end. /* else do */          
          end. /* for each tt-rqm-mstr */ 
           
          for each tt-rqm-mstr where tt-rqm-mstr.tt-prnbr <> "" and                      
             tt-rqm-mstr.tt-error = No no-lock:                                          
             find first rqm_mstr where rqm_nbr = tt-rqm-mstr.tt-prnbr no-error.          
             if not available rqm_mstr then leave.                                       
             find first rqpo_ref where rqpo_req_nbr = rqm_nbr                            
             no-lock no-error.                                                           
                                                                                         
             if available rqpo_ref then do:                                              
                run p_ip_msg (input 2081, input 3).                                      
                  /*CAN'T DELETE REQUISITION, REFERENCED BY POS*/                        
                undo, next.                                                              
             end.                                                                        
             line = 0.                                                                   
             for each rqd_det exclusive-lock where rqd_nbr = rqm_nbr and 
                rqd_line = tt-rqm-mstr.tt-prline:                    
/*GUI*/ if global-beam-me-up then undo, leave.
                for each cmt_det exclusive-lock                    
                    where cmt_indx = rqd_cmtindx:                  
                    delete cmt_det.                                
                end.                                               
                                                           
                /*DELETE ANY MRP DETAIL RECORDS*/                  
                for each mrp_det exclusive-lock                    
                    where mrp_dataset = "req_det"                  
                    and mrp_nbr = rqd_nbr                          
                    and mrp_line = string(rqd_line):               
/*GUI*/ if global-beam-me-up then undo, leave.
                    {inmrp.i                                                   
                     &part=mrp_det.mrp_part &site=mrp_det.mrp_site}            
                                                                        
                     delete mrp_det.                                           
                end.                                                       
/*GUI*/ if global-beam-me-up then undo, leave.
                delete rqd_det.
                line = line + 1.
             end.   /* for each rqd_det */
/*GUI*/ if global-beam-me-up then undo, leave.
             find first rqd_det where rqd_nbr = rqm_nbr no-lock no-error.
             if available rqd_det then next.
             for each cmt_det exclusive-lock
                 where cmt_indx = rqm_cmtindx:
                 delete cmt_det.
             end.
            /*SEND EMAILS*/
             email_sent_to = "".
             if not new_req then do:
                {gprun.i ""rqemsend.p""
                "(input recid(rqm_mstr), input ACTION_DELETE_REQ,
                  output email_sent_to)"}
/*GUI*/ if global-beam-me-up then undo, leave.
             end.

            /* WRITE HISTORY RECORD */
             {gprun.i ""rqwrthst.p""                
              "(input rqm_nbr,                      
                input 0,                            
                input ACTION_DELETE_REQ,            
                input userid_modifying,             
                input '',                           
                input email_sent_to)"}              
/*GUI*/ if global-beam-me-up then undo, leave.
             for each rqda_det exclusive-lock                            
                 where rqda_nbr = rqm_nbr:                               
                 delete rqda_det.                                        
             end.                                                        
             {gprunp.i "mcpl" "p" "mc-delete-ex-rate-usage"              
              "(input rqm_exru_seq)"}                                    
                                                                         
/*cli-cli             delete rqm_mstr.*/
          end.  /* for each tt-rqm-mstr where tt-prnbr <> "" */     
     end. /* repeat */
PROCEDURE p_ip_msg:
   define input parameter l_num  as integer no-undo.
   define input parameter l_stat as integer no-undo.
   {pxmsg.i &MSGNUM=l_num &ERRORLEVEL=l_stat}
END PROCEDURE. /* P_IP_MSG */


